package com.langsin.loujianguo.mapper;

import com.langsin.bean.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LQWQK
 * @version 1.0
 * @since 2022-06-26 14:37:13
 */
public class TestL {
    SqlSession sqlSession = null;


    @Test
    public void testUseGoOdsLazy() throws Exception {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user.getCname() + "," + user.getAddress());

        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders.getOrderId());

            try{Thread.sleep(3000);}catch(Exception e){e.printStackTrace();}

            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail.getDetailId());

                try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}
                System.out.println(orderDetail.getGoods());
            }
        }



    }



    @Test
    public void testUseGoOds() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(704);
        System.out.println(user.getCname() + "," +user.getAddress());
        try{Thread.sleep(5000);}catch(Exception e){e.printStackTrace();}

        // 5s以后 我才想获取 该用户 对应的 商品数据

        System.out.println("--------------------");
        for (Orders orders : user.getOrdersList()) {
            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail.getGoods());
            }
        }
    }


    @Test
    public void testOrdersUser() throws Exception {
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrdersId(1003);
        // 先获取 主表的信息
        System.out.println(orders.getOrderId() +" ~ "+orders.getOrderDate());

        // 模拟 程序 渡过了一段时间
        try{Thread.sleep(10000);}catch(Exception e){e.printStackTrace();}

        // 需要 获取 关联的用户的信息
        System.out.println(orders.getUser());

    }


    @Test
    public void testWs() throws Exception {
        Worker worker = new Worker();
        worker.setWid("1540669829059465216");
//        worker.setWname("大律师");
        worker.setAge(55);
//        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.updateWorkerByCondition(worker);
        System.out.println(row);
    }

    @Test
    public void test1M() throws Exception {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserOrders(702);
        System.out.println(user);

        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders + "!!!");
        }
    }

    @Test
    public void test3() throws Exception {
        Goods goods = new Goods();
        //goods.setGoodsName("%C%");
        goods.setShopPrice(18.0);
        //goods.setGoodsStock(900);

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goodsList = goodsMapper.queryByCondition(goods);

        goodsList.forEach(System.out::println);
    }

    @Test
    public void test2() throws Exception {
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(9003);
        ids.add(9008);
        ids.add(9009);

        List<Goods> goods = goodsMapper.queryByIds(ids);
        for (Goods good : goods) {
            System.out.println(good);
        }

        Goods goods1 = goodsMapper.queryByIdAno(9001);
        System.out.println(goods1);
    }

    @Test
    public void test1() throws Exception {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(702);
        System.out.println(user);
    }


    //每次单元测试类之前执行
    @Before
    public void before() throws Exception {
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话（连接）默认不提交
        sqlSession = sqlSessionFactory.openSession();
    }

    //每次单元测试类之后执行
    @After
    public void after() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }
}
